package com.neutech.shop.service.impl;

import com.neutech.shop.entity.Admin;
import com.neutech.shop.mapper.AdminMapper;
import com.neutech.shop.service.AdminService;
import com.neutech.shop.util.SsmUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/9/15 15:48
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;
    @Override
    public List<Admin> selectAll() {
        return adminMapper.selectAll();
    }

    @Override
    public void update(Admin admin,String originPassword, String newPassword) {
        admin = adminMapper.selectById(admin.getId());
        // 1. 原始密码必须和与数据库一致
        if(admin.getPassword().equals(SsmUtil.stringToMD5(originPassword))){
            // 2. 新密码也确认新密码一致
            admin.setPassword(newPassword);
            adminMapper.update(admin);
            return;
        }
        System.out.println("更新失败");
    }

    @Override
    public void delete(Long id) {
        adminMapper.delete(id);
        System.out.println("删除了id为" + id + "的数据。。。");
    }

    @Override
    public Admin selectById(Long id) {
        return adminMapper.selectById(id);
    }

    @Override
    public void resetPassword(Long id) {
        adminMapper.resetPassword(id);
    }

    @Override
    public boolean login(Admin admin) {
        String name = admin.getName();
        Admin adminServer = adminMapper.findByName(name);
        if(adminServer != null
                && adminServer.getPassword().equals(SsmUtil.stringToMD5(admin.getPassword()))){
            return true;
        } else{
            return false;
        }
    }
}
